// pages/cart/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cartGoodList: [],
        allChecked: false,
        totalPrice: 0,
        numTotal: 0
    },


    addAddress() {
        wx.chooseAddress({
            success: (res) => {
                wx.setStorageSync("address", res);
                this.setData({
                    address: res
                })
            },
        });
    },
    checkedChange(e) {
        let n = this.data.cartGoodList.findIndex(v => v.goods_id == e.target.dataset.id)
        this.data.cartGoodList[n].checked = !this.data.cartGoodList[n].checked
        let a = this.data.cartGoodList.filter(i => i.checked == true)
        // for (let i of a) {
        //     this.data.numTotal = 0,
        //         this.data.numTotal += i.num
        //     console.log(this.data.numTotal);
        // }
        this.data.numTotal = a.reduce((numTotal, v) => numTotal += v.num, 0)
        console.log(a);
        this.data.totalPrice = a.reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0)
        console.log(this.data.totalPrice);
        this.setData({
            allChecked: this.data.cartGoodList.length > 0 ? this.data.cartGoodList.every(v => v.checked == true) : false,
            totalPrice: this.data.totalPrice,
            numTotal: this.data.numTotal
        })
        wx.setStorageSync("cartGoodList", this.data.cartGoodList)
    },
    allCheckedChange() {
        this.setData({
            allChecked: !this.data.allChecked
        })
        this.data.cartGoodList.forEach(v => {
            v.checked = this.data.allChecked
        });
        this.setData({
            cartGoodList: this.data.cartGoodList
        })
        wx.setStorageSync("cartGoodList", this.data.cartGoodList)

        if (!this.data.allChecked) {
            this.setData({
                totalPrice: 0,
                numTotal: 0
            })

        } else {
            this.data.numTotal = 0
            let a = this.data.cartGoodList.filter(i => i.checked == true)
            for (let i of a) {
                this.data.numTotal += i.num
            }
            console.log(a);
            this.data.totalPrice = a.reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0)
            this.setData({
                totalPrice: this.data.totalPrice,
                numTotal: this.data.numTotal
            })

        }
    },

    numAdd(e) {
        this.data.numTotal = 0
        let id = this.data.cartGoodList.findIndex(i => i.goods_id == e.target.dataset.id)
        this.data.cartGoodList[id].num++
        let a = this.data.cartGoodList.filter(i => i.checked == true)
        console.log(a);
        this.data.totalPrice = a.reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0)
        for (let i of a) {
            this.data.numTotal += i.num
        }
        this.setData({
            cartGoodList: this.data.cartGoodList,
            totalPrice: this.data.totalPrice,
            numTotal: this.data.numTotal
        })
        wx.setStorageSync("cartGoodList", this.data.cartGoodList)
    },
    numReduce(e) {
        let id = this.data.cartGoodList.findIndex(i => i.goods_id == e.target.dataset.id)
        if (this.data.cartGoodList[id].num == 1) {
            wx.showModal({
                title: '提示',
                content: '你是否确定删除该商品',
                success: (res) => {
                    if (res.confirm) {
                        this.data.cartGoodList.splice(id, 1)
                        this.setData({
                            cartGoodList: this.data.cartGoodList,
                        })
                        wx.setStorageSync("cartGoodList", this.data.cartGoodList)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            this.data.cartGoodList[id].num--
        }
        this.data.numTotal = 0
        let a = this.data.cartGoodList.filter(i => i.checked == true)
        console.log(a);
        this.data.totalPrice = a.reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0)
        for (let i of a) {
            this.data.numTotal += i.num
        }
        this.setData({
            cartGoodList: this.data.cartGoodList,
            totalPrice: this.data.totalPrice,
            numTotal: this.data.numTotal
        })
        wx.setStorageSync("cartGoodList", this.data.cartGoodList)

    },
    buy() {
        if (!wx.getStorageSync("address").userName) {
            wx.showToast({
                title: '请您先添加收货地址',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.numTotal == 0) {
            wx.showToast({
                title: '请您选择要购买的商品',
                icon: 'none',
                duration: 1000
            })
            return
        }
        let payGood = wx.getStorageSync('cartGoodList').filter(i => i.checked)
        wx.setStorageSync('payGood', payGood)
        wx.navigateTo({
            url: "/pages/pay/index",
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
        });

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.data.numTotal = 0
        if (wx.getStorageSync("cartGoodList")) {
            let total = wx.getStorageSync("cartGoodList").reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0);
            this.setData({
                totalPrice: total,
            })
        }
        this.setData({
            address: wx.getStorageSync("address"),
            cartGoodList: wx.getStorageSync("cartGoodList") ? wx.getStorageSync("cartGoodList") : [],
            allChecked: wx.getStorageSync("cartGoodList").length > 0 ? wx.getStorageSync("cartGoodList").every(v => v.checked == true) : false,
        })
        let a = this.data.cartGoodList.filter(i => i.checked == true)
        for (let i of a) {
            this.data.numTotal += i.num
        }
        console.log(this.data.numTotal);
        this.setData({
            numTotal: this.data.numTotal
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})